package com.zwy.ubts.config;

import com.zwy.ubts.utils.DataInfo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author Zhou Wenying
 * @Description:
 * @Date Created in 22:29 2023/2/6
 */
@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ////1.获取session域对象
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpSession session = req.getSession();
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        String requestURI = req.getRequestURI();
        Object sissonUser = session.getAttribute("user");
        Object sissonAdmin = session.getAttribute("admin");


        //无条件放行
        if (requestURI.equals("/") ||
                requestURI.contains("/index.jsp") ||
                requestURI.contains("/login") ||
                requestURI.contains("/doLogin") ||
                requestURI.contains("/register") ||
                requestURI.contains("/doRegister") ||
                requestURI.contains("/getEMSCode") ||
                requestURI.contains("/updatePassword") ||
                requestURI.contains("/getUpdatePasswordCode") ||
                requestURI.contains("/sell") ||
                requestURI.contains("/getImg") ||



                requestURI.contains("/admin/AdminLogin") ||
                requestURI.contains("/admin/doAdminLogin") ||
                requestURI.contains("/static")
        ){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        //判断是否是访问admin相关的
        if(requestURI.contains("/admin/")){
            if (sissonAdmin != null){
                //访问admin 又登录了
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                //没登录
                req.setAttribute("msg","请先登录！！！");
                req.getRequestDispatcher(DataInfo.PAGE_ROOT+"admin.jsp").forward(req,resp);
            }
            return;
        }

        //处理管理员可以访问普通页面
        if (sissonAdmin != null &&
                (
                    requestURI.contains("/showBook") // ||
                )
        ){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        //上面处理了管理员 下面就是用户
        if(sissonUser != null){
            //用户登录了
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            req.setAttribute("msg","请先登录！！！");
            req.getRequestDispatcher(DataInfo.PAGE_ROOT+"login.jsp").forward(req,resp);
        }

    }

    @Override
    public void destroy() {

    }
}
